package com.fh.shop.api.member.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.Constant;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.member.biz.IMemberService;
import com.fh.shop.api.member.param.EditPwdParam;
import com.fh.shop.api.member.param.MemberLoginParam;
import com.fh.shop.api.member.param.MemberParam;
import com.fh.shop.api.member.vo.MemberInfoVo;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/api/member")
@Api(tags = "会员接口",description = "会员的前台API接口项目")
public class MemberController {
    @Resource(name = "memberService")
    private IMemberService memberService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @Value("${member.activa.success}")
    private String success;

    @Value("${member.activa.error}")
    private String error;

    //注册
    @ApiOperation("会员的注册接口")
    @PostMapping
    public ServerResponse reg(@RequestBody MemberParam memberParam){
        return memberService.reg(memberParam);
    }

    //登录
    @ApiOperation("会员的登录接口")
    @PostMapping("/login")
    public ServerResponse login(MemberLoginParam memberLoginParam){
        return memberService.login(memberLoginParam);
    }

    //登录昵称
    @GetMapping
    @NeedLogin
    public ServerResponse loginNickName(){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        String nickName = memberVo.getNickName();
        Long memberId = memberVo.getId();
        String key = KeyUtil.CartKey(memberId);
        String count = RedisUtil.hget(key, Constant.MEMBER_CART_COUNT);
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        memberInfoVo.setName(nickName);
        memberInfoVo.setCount(count);
        return ServerResponse.success(memberInfoVo);
    }

    //注销
    @ApiOperation("会员的注销接口")
    @PostMapping("/logout")
    @NeedLogin
    public ServerResponse logout(){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long id = memberVo.getId();
        RedisUtil.del(KeyUtil.memberKey(id));
        return ServerResponse.success();
    }

    //修改密码
    @PostMapping("/editPwd")
    @NeedLogin
    public ServerResponse editPwd(EditPwdParam editPwdParam){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long id = memberVo.getId();
        editPwdParam.setId(id);
        RedisUtil.del(KeyUtil.memberKey(id));
        return memberService.editPwd(editPwdParam);
    }

    //激活会员
    @GetMapping("/activa/{uuid}")
    public void activa(@PathVariable String uuid) throws IOException {
        int status = memberService.updateStatus(uuid);
        if (status == Constant.ACTIVA_SUCCESS){
            response.sendRedirect(success);
        }else {
            response.sendRedirect(error);
        }
    }

    //激活按钮
    @PostMapping("/memberActiva")
    public ServerResponse memberActiva(String mail){
        return memberService.memberActiva(mail);
    }

    //根据邮箱激活会员
    @GetMapping("/activaByMail")
    public void activaByMail(String uuid) throws IOException {
        boolean status = memberService.updateStatusByMail(uuid);
        if (status){
            response.sendRedirect(success);
        }else {
            response.sendRedirect(error);
        }
    }

}
